package org.csu.mypetstore.controller;

import com.sun.org.apache.xml.internal.dtm.ref.sax2dtm.SAX2RTFDTM;
import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.service.AccountService;
import org.csu.mypetstore.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/adminUser")
public class AdminUserController {
    @Autowired
    AccountService accountService;

    @Autowired
    AdminService adminService;

    //跳转到修改密码的页面
    @GetMapping("/viewEditPassword")
    public String viewEditPassword(String username, Model model){
        model.addAttribute("username",username);

        return "EditUser/EditPassword";
    }

    //修改用户的密码
    @PostMapping("/editPassword")
    public String editPassword(HttpServletRequest req, Model model, String username){
        String password = req.getParameter("password");
        String repeatedPassword = req.getParameter("repeatedPassword");

        if(!password.equals(repeatedPassword)){
            String msg = "The passwords are not the same";
            return "EditUser/EditPassword";
        }
        else {
            accountService.updatePassword(username,password);

            List<Account> userList=accountService.getAccountList();
            model.addAttribute("userList",userList);

            return  "EditUser/EditUserInfo";
        }
    }

    //禁用用户的权限
    @GetMapping("/banAccount")
    public String banAccount(Model model, String username){
        adminService.banAccount(username);

        List <Account> userList=accountService.getAccountList();
        model.addAttribute("userList",userList);

       return "EditUser/EditUserInfo";
    }

    //解禁用户权限
    @GetMapping("/unbanAccount")
    public String unbanAccount(Model model, String username){
        adminService.unbanAccount(username);

        List <Account> userList=accountService.getAccountList();
        model.addAttribute("userList",userList);

        return "EditUser/EditUserInfo";
    }


}
